<template>
  <el-dialog
    :width="'90%'"
    :visible="dialogVisible"
    title="集群信息"
    @close="$emit('close')"
  >
    <el-pagination
      style="float: right;"
      layout="prev, pager, next,sizes, total"
      :total="total"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      :current-page="page+1"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-table
      v-loading="loading"
      :data="data"
    >
      <el-table-column
        prop="cmName"
        label="集群"
        overflow-tooltip
      />
      <el-table-column
        prop="clusterName"
        label="子集群"
        overflow-tooltip
      />
      <el-table-column
        prop="hostCnt"
        label="主机"
      >
        <template slot-scope="scope">
          <span
            style="padding: 0.6rem;"
            class="clickable"
            @click="()=>{
              selectedRow = scope.row
              hostDialogVisible = true
            }"
          >
            {{ scope.row.hostCnt }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="roleCnt"
        label="角色"
      >
        <template slot-scope="scope">
          <span 
            style="padding: 0.6rem;"
            class="clickable"
            @click="()=>{
              selectedRow = scope.row
              roleDialogVisible = true
            }"
          >
            {{ scope.row.roleCnt }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="serviceCnt"
        label="服务"
      >
        <template slot-scope="scope">
          <span
            style="padding: 0.6rem;"
            class="clickable"
            @click="()=>{
              selectedRow = scope.row
              serviceDialogVisible = true
            }"
          >
            {{ scope.row.serviceCnt }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="cpuMax"
        label="CPU"
      >
        <template slot-scope="scope">
          <CellProgress
            :title="scope.row.cpuMax+'个'"
            :percentage="Math.round(+scope.row.cpuUsed)"
            @click="()=>{
              selectedStatistic = 'CPU'
              selectedRow = scope.row
              statisticsDialogVisible = true
            }"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="memMax"
        label="内存"
      >
        <template slot-scope="scope">
          <CellProgress
            :percentage="Math.round(+scope.row.memUsage*100)"
            :title="`${scope.row.memUsed}/${scope.row.memMax}`"
            @click="()=>{
              selectedStatistic = '内存'
              selectedRow = scope.row
              statisticsDialogVisible = true
            }"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="存储"
      >
        <template slot-scope="scope">
          <CellProgress
            :percentage="Math.round(+scope.row.hdfsUsage*100)"
            :title="`${scope.row.hdfsUsed}/${scope.row.hdfsMax}`"
            @click="()=>{
              selectedStatistic = '存储'
              selectedRow = scope.row
              statisticsDialogVisible = true
            }"
          />
        </template>
      </el-table-column>
    </el-table>

    <HostDialog
      :dialog-visible="hostDialogVisible"
      :cm-flag="selectedRow ? selectedRow.cmFlag : ''"
      :cluster-flag="selectedRow ? selectedRow.clusterFlag : ''"
      :cm-name="selectedRow ? selectedRow.cmName : ''"
      :cluster-name="selectedRow ? selectedRow.clusterName : ''"
      @close="hostDialogVisible = false"
    />

    <ServiceDialog
      :dialog-visible="serviceDialogVisible"
      :cm-flag="selectedRow ? selectedRow.cmFlag : ''"
      :cluster-flag="selectedRow ? selectedRow.clusterFlag : ''"
      :cm-name="selectedRow ? selectedRow.cmName : ''"
      :cluster-name="selectedRow ? selectedRow.clusterName : ''"
      @close="serviceDialogVisible = false"
    />

    <RoleDialog
      :dialog-visible="roleDialogVisible"
      :cm-flag="selectedRow ? selectedRow.cmFlag : ''"
      :cluster-flag="selectedRow ? selectedRow.clusterFlag : ''"
      :cm-name="selectedRow ? selectedRow.cmName : ''"
      :cluster-name="selectedRow ? selectedRow.clusterName : ''"
      @close="roleDialogVisible = false"
    />

    <StatisticsDialog
      :dialog-visible="statisticsDialogVisible"
      :name="selectedStatistic"
      :cm-flag="selectedRow ? selectedRow.cmFlag : ''"
      :cluster-flag="selectedRow ? selectedRow.clusterFlag : ''"
      @close="statisticsDialogVisible = false"
    />
  </el-dialog>
  
</template>

<script>
import overviewApi from '@/api/monitor/overview'
import HostDialog from './HostDialog.vue'
import ServiceDialog from './ServiceDialog.vue'
import RoleDialog from './RoleDialog.vue'
import CellProgress from './CellProgress.vue'
import StatisticsDialog from './StatisticsDialog.vue'

export default {
  components: {
    HostDialog,
    ServiceDialog,
    RoleDialog,
    CellProgress,
    StatisticsDialog
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  emits: ['close'],
  data() {
    return {
      page: 0,
      size: 10,
      data: [],
      total: 0,
      loading: false,
      hostDialogVisible: false,
      serviceDialogVisible: false,
      roleDialogVisible: false,
      statisticsDialogVisible: false,
      selectedRow: null,
      selectedStatistic: ''
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.page = 0
        this.size = 10
        this.getData()
      }
    }
  },
  methods: {
    getData(resetPage = true) {
      this.loading = true
      this.page = resetPage ? 0 : this.page
      const params = {
        page: this.page,
        size: this.size
      }
      overviewApi.homePage(params).then(res => {
        this.data = res.content
        this.total = res.totalElements
      }
      ).finally(() => {
        this.loading = false
      })
    },
    handleCurrentChange(page) {
      this.page = page - 1
      this.getData(false)
    },
    handleSizeChange(size) {
      this.size = size
      this.getData(true)
    }
  }
}
</script>

<style lang="scss" scoped>
.clickable {
  cursor: pointer;
  color:#3F9EFF
}
</style>
